﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MegavaniaX
{
    /// <summary>
    /// Event types for Megavania.
    /// </summary>
    public enum MegavaniaEventType
    {
        PlayerDamaged,
        ShotFired,

        // Animations
        AnimationStart,
        AnimationComplete,

        // User input
        MoveLeftPressed,
        MoveLeftReleased,
        MoveRightPressed,
        MoveRightReleased,
        ShootPressed,
        ShootReleased,        
        JumpPressed,
        JumpReleased,
        SlidePressed,
        ClimbPressed,

        // Camera stuff
        CameraZoomChange,
        CameraMoveUpStart,
        CameraMoveUpFinished,
        CameraMoveDownStart,
        CameraMoveDownFinished,
        CameraMoveRightStart,
        CameraMoveRightFinished,
        CameraMoveLeftStart,
        CameraMoveLeftFinished,

        // Weapons
        WeaponChangeStarted,

        // Sound event
        PlaySound,

        // Spawning
        SpawnActor,
        DespawnActor,

        // Enemy
        EnemyDied,

        // Screen transistions
        ScreenTransistionStart,
        ScreenTransistionEnd,
    }

    public delegate void MegavaniaEventCallback (IMegavaniaEvent evt);

    public static class MessagingSystem
    {
        public static MessagingHelper helper = new MessagingHelper();

        private static Queue<IMegavaniaEvent> queue = new Queue<IMegavaniaEvent>();

        /// <summary>
        /// Registers an event listener to listen to events of the specified type.
        /// </summary>
        /// <param name="eventType">The type of event to listen to.</param>
        public static void SubscribeToEvent(MegavaniaEventCallback callback, MegavaniaEventType type, string myName)
        {
            helper.Subscribe(callback, type, myName);
        }

        /// <summary>
        /// Dispatches an event into the system... now by queueing it.
        /// </summary>
        /// <param name="megavaniaEvent"></param>
        public static void DispatchEvent(IMegavaniaEvent evt)
        {
            queue.Enqueue(evt);
        }

        public static void ProcessQueued()
        {
            while (queue.Count > 0)
            {
                var evt = queue.Dequeue();
                helper.DispatchEvent(evt);
            }
        }
    }
}
